import {defineStore} from 'pinia';
import Cookies from 'js-cookie';
import {Session} from '/@/utils/storage';
import {userInfo} from "/@/api/buss/user";

const userApi = userInfo();

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: (): UserInfosState => ({
		userInfos: {
			userName: '',
			photo: '',
			time: 0,
			roles: [],
			authBtnList: [],
		},
	}),
	actions: {
		async setUserInfos() {
			// 存储用户信息到浏览器缓存
			if (Session.get('userInfo')) {
				this.userInfos = Session.get('userInfo');
			} else {
				try {
					const response = await userApi.queryUserInfo();
					this.userInfos = response.data; // Assuming the actual data is in response.data
					console.log(this.userInfos)
				} catch (error) {
					console.error('Error fetching user info:', error);
				}
			/*	this.userInfos = <UserInfos>await userApi.queryUserInfo().data;
				console.log(await userApi.queryUserInfo().data)*/
			}
		},
	},
});
